<template>
  <j-modal
    :title="title"
    :visible="visible"
    @cancel="handleCancel"
    @ok="handleOk"
    width="40%"
    :confirmLoading="confirmLoading"
    :okText="$t('保存')"
    :cancelText="$t('关闭')"
    v-drag
  >
    <div>
      <a-form-model
        :rules="rules"
        :model="model"
        ref="form"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
        v-enterToNext
      >
        <a-row>
          <a-col :span="24">
            <a-form-model-item :label="$t('商户ID')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantId">
              <a-input v-model="model.merchantId"></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('商户KEY')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="merchantKey">
              <a-input v-model="model.merchantKey"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('货币编码')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="currencyCode">
              <a-input v-model="model.currencyCode"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('渠道编码')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="channelCode">
              <a-input v-model="model.channelCode"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item :label="$t('请求地址')" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="apiUrl">
              <j-dict-select-tag type="list" v-model="model.apiUrl" dictCode="bis_pay_channel_2c2p.api_url" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
  </j-modal>
</template>

<script>
import { httpAction, getAction } from '@/api/manage'
export default {
  name: 'CpModal',
  components: {},
  data() {
    return {
      model: {},
      title: '',
      visible: false,
      confirmLoading: false,
      labelCol: {
        xs: { span: 20 },
        sm: { span: 4 },
      },
      wrapperCol: {
        xs: { span: 20 },
        sm: { span: 18 },
      },
      rules: {
        merchantId: [
          { required: true, message: this.$t('请输入')},
        ],
        merchantKey: [
          { required: true, message: this.$t('请输入')},
        ],
        currencyCode: [
          { required: true, message: this.$t('请输入')},
        ],
        channelCode: [
          { required: true, message: this.$t('请输入')},
        ],
        apiUrl: [
          { required: true, message: this.$t('请选择')},
        ],
      },
      url: {
        add: '/pay/bisPayChannel2c2p/add',
        edit: '/pay/bisPayChannel2c2p/edit',
      },
    }
  },
  methods: {
    edit() {
      let that = this
      that.visible = true
      getAction('/pay/bisPayChannel2c2p/list').then((res) => {
        if (res.success) {
          if (res.result.records.length != 0) {
            that.model = Object.assign({}, res.result.records[0])
          } else {
            that.model = {
            }
          }
        }
      })
    },
    handleOk() {
      let that = this
      that.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true
          let url = ''
          let method = ''
          if (that.model.id) {
            url = that.url.edit
            method = 'put'
          } else {
            url = that.url.add
            method = 'post'
          }
          httpAction(url, that.model, method)
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.close()
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
    close() {
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
  },
}
</script>
<style scoped>
</style>